package com.example.parion;


import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.parion.DatabaseModel.DataFields;

public class MyPeopleActivity extends FragmentActivity implements LoaderCallbacks<Cursor> {

	ListView list;
	ImageView btn;
	SimpleCursorAdapter mAdapter;
	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.mypeople_activity);
	    // TODO Auto-generated method stub
	    list = (ListView)findViewById(R.id.listView1);
	    
	    mAdapter = new SimpleCursorAdapter(this, 
	    		R.layout.mypeople_item, 
	    		null, 
	    		new String[] {DatabaseModel.DataFields.People.NAME , DatabaseModel.DataFields.People.PHONE,
	    		DatabaseModel.DataFields.People.KAKAO_NAME, DatabaseModel.DataFields.People.NATEON_NAME}, 
	    		new int[] {R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4});
	    
	    list.setAdapter(mAdapter);
	    
	    getSupportLoaderManager().initLoader(0, null, this);
	    
	    btn = (ImageView)findViewById(R.id.btn_add_mypeople);
	    btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i =  new Intent(MyPeopleActivity.this, AddMyPeopleActivity.class);
				startActivity(i);
			}
		});
	    
	    
	    
	    
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
		// TODO Auto-generated method stub
		
		String selection = null;
		String[] args = null;
		String name= null;
		String[] projection= {DataFields.People.ID,
				DataFields.People.NAME,
				DataFields.People.PHONE,
				DataFields.People.KAKAO_NAME,
				DataFields.People.NATEON_NAME};
		
//		if (name != null && !name.equals("")) {
//			selection = DataFields.People.NAME + " = ?";
//			args = new String[] {name};
//		}
		
		MyDBLoader loader = new MyDBLoader(this);
	    loader.setParameter(DataFields.People.TABLE_NAME, projection, selection, args, null, null, null, null, false);
	    
	    return loader;
		
		
	}

	@Override
	public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
		// TODO Auto-generated method stub
		mAdapter.changeCursor(arg1);
	}

	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		// TODO Auto-generated method stub
		
	}

}
